/**
 * Created by yhg97p on 2016/9/27.
 */
define(['app', 'filters/filters'], function(app) {
	app.register.controller('compMember', function($scope, $http, $state, $sce, $stateParams, $rootScope,$Ajax) {
			$scope.loading_show = true;
			$scope.member_blank = true;

			$scope.load = {
				_getMajorinfo: function(){  //获取公司信息
					var url = app._baseUrl + '/QyManage/Setting/detail';
					var data = {};
					$Ajax.postData(url,data).then(function(res){
                        if(res.data.success){
                            $scope.loading_show = false;
                            $scope.memberinfo = res.data.data;
                            if($scope.memberinfo.menber_num == $scope.memberinfo.menber_total) {
                                $scope.addMember = function() {
                                    $scope.tipModal = {
                                        isShow: false,
                                        title: '温馨提示',
                                        data: {},
                                        confirm: function() {
                                            this.hide()
                                        },
                                        cancel: function() {
                                            this.hide()
                                        },
                                        hide: function() {
                                            this.isShow = false;
                                        },
                                        show: function() {
                                            this.isShow = true;
                                        }
                                    }
                                    $scope.tipModal.show();
                                }
                            }
                        }
					});
				},
				_getUserindex:function(){//获取成员列表
					var url = app._baseUrl + '/QyManage/User/index';
					var data = {};
					$Ajax.postData(url,data).then(function(res){
                        if(res.data.success) {
                            $scope.member_blank = false;
                            $scope.items = res.data.data;
                            //console.log($scope.items)
                            $rootScope.admini=[];$rootScope.boss=[];$rootScope.membe=[];
                            angular.forEach($scope.items, function(data) {
                                if(data.role_id==2){
                                    $rootScope.admini.push(data)
                                }
                                if(data.role_id==1){
                                    $rootScope.boss.push(data)
                                }
                                if(data.role_id==0){
                                    $rootScope.membe.push(data)
                                }
                                if(data.status == 1) {
                                    data.statue_name = "已加入";
                                }
                                if(data.status == 0) {
                                    data.statue_name = "待加入";
                                }

                                if(data.status == 2) {
                                    data.statue_name = "已退出";

                                }
                                if(data.role_id == 0) {
                                    data.role_name = "成员";
                                }
                                if(data.role_id == 1) {
                                    data.role_name = "拥有者";
                                    $scope.out_info = false;

                                }
                                if(data.role_id == 2) {
                                    data.role_name = "管理员";

                                }
                            })
                            $scope.allmember = true;
                            $scope.administr = false;
                        } else {
                            $scope.member_blank = true;
                        }
					})
				},
				init: function(){
					this._getMajorinfo && this._getMajorinfo();
					this._getUserindex && this._getUserindex();
				}
			};
			$scope.load.init && $scope.load.init();

				//管理员
				$scope.admini = function() {
					$('.nav_left').find('a').removeClass('active');
					$('.nav_left').eq('1').find('a').addClass('active');
						$scope.member_blank = false;
						$scope.admin = $rootScope.admini;
						$scope.admin.len = $scope.admin.length;					
						$scope.allmember = false;
						$scope.administr = true;
			
				}
				//所有成员
				$scope.all = function() {
					$('.nav_left').find('a').removeClass('active');
					$('.nav_left').eq('0').find('a').addClass('active');				
					$scope.member_blank = false;
					$scope.allmember = true;
					$scope.administr = false;
				}
				//成员
				$scope.memberinf = function() {
					$('.nav_left').find('a').removeClass('active');
					$('.nav_left').eq('2').find('a').addClass('active');
					$scope.admin = [];
						$scope.member_blank = false;
						$scope.admin = $rootScope.membe;
						$scope.admin.len = $scope.admin.length;
						$scope.allmember = false;
						$scope.administr = true;
		
				}

				//下面的是岗位列表

				$http({
					url: app._baseUrl + '/QyManage/User/cateList',
					method: 'POST',
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					}
				}).success(function(res) {
					var arr=[];
					arr=res.data.slice(0,4);
					$scope.cateinfo=arr;
				});
                
                $scope.cate = function(v,k) {
                    	$scope.admin = [];
						$('.nav_left').find('a').removeClass('active');
						$('.nav_left').eq(k+3).find('a').addClass('active');				
							$scope.member_blank = false;
							$rootScope.arr=[];
							angular.forEach($scope.items, function(data) {						
								if(data.cate_id==k+1){
									$rootScope.arr.push(data)
								}
							});
							$scope.admin = $rootScope.arr;
							$scope.admin.len = $scope.admin.length;
							$scope.allmember = false;
							$scope.administr = true;
				
                   }
					//进行编辑、删除和添加
				$scope.seclec=function(){
						$scope.sel=false;
                        // $("#selectRole option[value='']").remove(); //删除Select中Value=''的Option
					}
				$scope.edit = function(item) {
						//console.log(item);
						//console.log(item.cate_id)
						if(item.cate_id==null){
                            item.cate_id = 0;
						}
						if(item.role_id == 0) {
							$scope.rolenum = 0
						}
						if(item.role_id == 2) {
							$scope.rolenum = 2
						}
						if(item.cate_id == 1) {
							$scope.catenum = 1
						}
						if(item.cate_id == 2) {
							$scope.catenum = 2
						}
						if(item.cate_id == 3) {
							$scope.catenum = 3
						}
						if(item.cate_id == 4) {
							$scope.catenum = 4
						}
						//console.log(item.cate_id)
						$scope.editModal = {
							isShow: false,
							title: '编辑成员',
                            cate_id: item.cate_id,
							phone: item.phone,
							rolename: item.role_id,
							catename: item.cate_name,
							position:item.position,
							confirm: function() {
								var role = $.trim($('.modal-content select[name="selectRole"]').val()),
									post = $.trim($('.modal-content select[name="selectPost"]').val()),
									phone = $.trim($('.modal-content input[name="phone"]').val())
								$http({
									url: app._baseUrl + '/QyManage/User/edit',
									method: 'POST',
									data: {
										relat_id: item.relat_id,
										role_id: role,
										cate_id: $scope.editModal.cate_id,
										position:$scope.editModal.position
									},
									headers: {
										'Content-Type': 'application/x-www-form-urlencoded'
									}
								}).success(function(res) {
									if(res.success) {
										console.log(1);
										$scope.editinfo = res.data;
                                        $scope.editModal.hide()
										$scope.all();
                                        $scope.load._getUserindex && $scope.load._getUserindex();
									} else {
										//console.log(res.message)
										$('.tips').text(res.message).show();
									}
								});

							},
							cancel: function() {
								this.hide()
							},
							hide: function() {
								this.isShow = false;
							},
							show: function() {
								this.isShow = true;
							}
						}
                        //console.log($scope.editModal.catename);
						$scope.editModal.show();
					}

	
				$scope.addMember = function() {
						$scope.roles = '';
						$scope.position = '';
						$scope.pho = '';
						$scope.addModal.show();

					};

				//删除
				$scope.deletememeber = function(item) {
						$scope.deleinfo = item;
						$scope.deletelModal = {
							isShow: false,
							title: '移除成员',
							data: item.user_name,
							confirm: function() {
								$http({
									url: app._baseUrl + '/QyManage/User/remove',
									method: 'POST',
									data: {
										relat_id: item.relat_id
									},
									headers: {
										'Content-Type': 'application/x-www-form-urlencoded'
									}
								}).success(function(res) {
									if(res.success) {
										$scope.addinfo = res.data;
										parseInt($scope.memberinfo.menber_num);
										$scope.memberinfo.menber_num -= 1;
									}
								});
								this.hide();
								$scope.all();
                                $scope.load.init && $scope.load.init();;
							},
							cancel: function() {
								this.hide()
							},
							hide: function() {
								this.isShow = false;
							},
							show: function(item) {
								this.isShow = true;
							}
						}
						$scope.deletelModal.show();
					};
                $scope.$watch('roles',function (n,o){
                    if(n!=o){
                       if(n==2){
                       	$scope.posts=0;
					   }
					  if(n==0){
                          $scope.posts="";
					  }
                    }
                });
                $scope.$watch('editModal.rolename',function (n,o){
                	//console.log(o)
                    if(n!=o){
                        if(n==2){
                            $scope.editModal.cate_id=0;
                        }
                        if(n==0&&o!=undefined){
                            $scope.editModal.cate_id=1;
                        }
                    }
                });
                    //添加
					$scope.addModal = {
						isShow: false,
						title: '添加成员',
						data: {},
						confirm: function() {
							if($scope.memberinfo.menber_num==$scope.memberinfo.menber_total){
								return false;
							}
							var role = $.trim($('.modal-content select[name="selectRole"]').val());
							var post = $.trim($('.modal-content select[name="selectPost"]').val());
							var phone = $.trim($('.modal-content input[name="phone"]').val());
                            if(role==''){
                                $('.tips').text("请选择角色").show();
                                return false;
                            }
							if(post==''){
                                $('.tips').text("请选择岗位").show();
								return false;
							}
                            $scope.exp = /^1[3|4|5|7|8][0-9]{9}$/;
                            if(phone.length == 0) {
                                $('.tips').text("账号码不能为空").show();
                                return false
                            } else {
                                if($scope.exp.test(phone) == false) {
                                    $('.tips').text("您输入的号码格式有误").show();
                                    return false;
                                }
                            }
                            $http({
								url: app._baseUrl + '/QyManage/User/add',
								method: 'POST',
								data: {
									cate_id: post,
									role_id: role,
									phone: phone,
                                    position:$scope.position
								},
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded'
								}
							}).success(function(res) {
								if(res.success) {
									$scope.addinfo = res.data;
									$scope.addModal.isShow = false;
									$scope.all();
									parseInt($scope.memberinfo.menber_num);
									$scope.memberinfo.menber_num += 1;
                                    $scope.load.init && $scope.load.init();
								} else {
									$scope.exp = /^1[3|4|5|7|8][0-9]{9}$/;
									if(phone.length == 0) {
										$('.tips').text("账号码不能为空").show();
									} else {
										if($scope.exp.test(phone) == false) {
											$('.tips').text("您输入的号码格式有误").show();
										} else {
											$('.tips').text(res.message).show();
										}
									}
								}

							});

						},
						cancel: function() {
							this.hide()
						},
						hide: function() {
							this.isShow = false;
						},
						show: function() {
							this.isShow = true;
						}
					}
				})
});